package com.example.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    public static String getMD5Hash(String input) {
        try {
            // 1. 创建实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 2. 计算哈希值
            byte[] hashBytes = md.digest(input.getBytes());

            // 3. 将字节数组转换为16进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }

            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }
}
